


export class DateUtils {
    /** 
     * 格式化日期
     * @param {Number | Date | String} timestamp
     * @param {String} strFormat
     * @returns {String}
     */
    static dateFormat = (timestamp, strFormat = "YYYY-MM-DD") => {
        let result = '';
        if (typeof timestamp === 'string') {
            timestamp = +new Date(timestamp);
        }
        const myDate = new Date(+timestamp);
        const YYYY = myDate.getFullYear() + "";
        const MM = ("00" + (myDate.getMonth() + 1)).slice(-2);
        const DD = ("00" + myDate.getDate()).slice(-2);
        const HH = ("00" + myDate.getHours()).slice(-2);
        const mm = ("00" + myDate.getMinutes()).slice(-2);
        const ss = ("00" + myDate.getSeconds()).slice(-2);
        const sss = ("" + myDate.getMilliseconds());
        // 星期
        const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        const day = days[myDate.getDay()];
        result = strFormat
            .replace(/YYYY/, YYYY)
            .replace(/MM/, MM)
            .replace(/DD/, DD)
            .replace(/HH/, HH)
            .replace(/mm/, mm)
            .replace(/ss/, ss)
            .replace(/sss/, sss)
            .replace(/day/, day);
        return result;
    }

	/** 时间字符串 转换时间  2024-12-20 00:00:00 => Date */
	static strToDate = (dateString) => {
		let year = 0;
		let month = 0;
		let day = 0;
		let hours = 0;
		let minutes = 0;
		let seconds = 0;
		
		dateString.replace(
			/(\d{4})-(\d{1,2})-(\d{1,2})[\s|T]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?/i,
			(_input, _year, _month, _day, _hours, _minutes, _seconds) => {
				console.log(`input:${_input}`);
				console.log(`year:${_year}`);
				console.log(`month:${_month}`);
				console.log(`day:${_day}`);
				console.log(`hours:${_hours}`);
				console.log(`minutes:${_minutes}`);
				console.log(`seconds:${_seconds}`);
				return '';
			});
	};

    /** 
     * 是否是同一天
     * @param {Number | Date | String} dateValue1
     * @param {Number | Date | String} dateValue2
     * @returns {Boolean}
     *  */
    static isSameDay(dateValue1, dateValue2) {
        const date1 = new Date(dateValue1);
        const date2 = new Date(dateValue2);
        return date1.getFullYear() === date2.getFullYear() &&
            date1.getMonth() === date2.getMonth() &&
            date1.getDate() === date2.getDate();
    }

    /** 显示计时器时间 00:00:00
     * @param {Number} duration 秒
     * @returns {String}
     */
    static displayTimer = (duration) => {
        const h = Math.floor(duration / 3600);
        const mm = Math.floor((duration - h * 3600) / 60);
        const ss = duration % 60;
        const strMm = ('00' + mm).slice(-2);
        const strSs = ('00' + ss).slice(-2);
        const result = `${h}:${strMm}:${strSs}`
        return result;
    }
}